/*
 * Copyright (c) 2008 Benoit Bouchez, Nicolas Giard
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.noocodecommit.wicket.stuff.jquery.plugin.ui.colorpicker;

import com.noocodecommit.wicket.stuff.jquery.plugin.JQPlugin;

/**
 * 
 * @author Benoit Bouchez
 */
public class JQColorPickerUtils
{

	/**
	 * Create a JQColorPickerBuilder instance on which you can fluently change settings
	 * 
	 * @param id
	 *            wicket component id
	 * @param key
	 *            plugin unique id on which component is attached
	 * @return JQColorPickerBuilder
	 */
	public static JQColorPickerBuilder create(String id, String key)
	{
		return new JQColorPickerBuilder(new JQColorPicker(id, (key == null ? JQColorPickerPlugin.DEFAULT_KEY : key)));
	}

	/**
	 * Create a JQColorPickerBuilder instance on which you can fluently change settings
	 * 
	 * @param id
	 *            wicket component id
	 * @return JQColorPickerBuilder
	 */
	public static JQColorPickerBuilder create(String id)
	{
		return new JQColorPickerBuilder(new JQColorPicker(id, null));
	}

	/**
	 * @author Benoit Bouchez
	 */
	public static final class JQColorPickerBuilder
	{
		/** JQuery component */
		private JQColorPicker component;
		/** JQuery default options */
		private JQColorPickerOptions options;

		/**
		 * Constructor
		 * 
		 * @param component
		 *            widget
		 */
		public JQColorPickerBuilder(JQColorPicker component)
		{
			this.component = component;
			this.options = new JQColorPickerOptions();
		}

		/**
		 * Sets plugin init mode
		 * 
		 * @param initMode
		 *            plugin init mode
		 * @return JQColorPickerBuilder
		 */
		public JQColorPickerBuilder initMode(int initMode)
		{
			component.getPlugin().setInitMode(initMode);
			return this;
		}

		/**
		 * Sets plugin selector when initMode is PLUGIN_INIT_MODE_ONCE
		 * 
		 * @param initOnceSelector
		 *            JQuery selector
		 * @return JQColorPickerBuilder
		 */
		public JQColorPickerBuilder initOnceSelector(String initOnceSelector)
		{
			component.getPlugin().setInitOnceSelector(initOnceSelector);
			return this;
		}

		/**
		 * Sets the eventName option value
		 * 
		 * @param eventName
		 *            colorpicker eventName
		 * @return JQColorPickerBuilder
		 */
		public JQColorPickerBuilder eventName(String eventName)
		{
			options.setEventName(eventName);
			return this;
		}

		/**
		 * Sets the color option value
		 * 
		 * @param color
		 *            colorpicker color
		 * @return JQColorPickerBuilder
		 */
		public JQColorPickerBuilder color(String color)
		{
			options.setColor(color);
			return this;
		}

		/**
		 * Sets the flat option value
		 * 
		 * @param flat
		 *            colorpicker flat
		 * @return JQColorPickerBuilder
		 */
		public JQColorPickerBuilder flat(boolean flat)
		{
			options.setFlat(flat);
			return this;
		}

		/**
		 * Sets the show function
		 * 
		 * @param showScript
		 *            colorpicker show function
		 * @return JQColorPickerBuilder
		 */
		public JQColorPickerBuilder showScript(String showScript)
		{
			options.setShowScript(showScript);
			return this;
		}

		/**
		 * Sets the beforeShow function
		 * 
		 * @param beforeShowScript
		 *            colorpicker beforeShow function
		 * @return JQColorPickerBuilder
		 */
		public JQColorPickerBuilder beforeShowScript(String beforeShowScript)
		{
			options.setBeforeShowScript(beforeShowScript);
			return this;
		}

		/**
		 * Sets the change function
		 * 
		 * @param changeScript
		 *            colorpicker change function
		 * @return JQColorPickerBuilder
		 */
		public JQColorPickerBuilder changeScript(String changeScript)
		{
			options.setChangeScript(changeScript);
			return this;
		}

		/**
		 * Sets the submit function
		 * 
		 * @param submitScript
		 *            colorpicker submit function
		 * @return JQColorPickerBuilder
		 */
		public JQColorPickerBuilder submitScript(String submitScript)
		{
			options.setSubmitScript(submitScript);
			return this;
		}

		/**
		 * Sets the hide function
		 * 
		 * @param hideScript
		 *            colorpicker hide function
		 * @return JQColorPickerBuilder
		 */
		public JQColorPickerBuilder hideScript(String hideScript)
		{
			options.setHideScript(hideScript);
			return this;
		}

		/**
		 * @return JQColorPicker
		 */
		public JQColorPicker build()
		{
			if (component.getPlugin().getInitMode() == JQPlugin.PLUGIN_INIT_ONCE)
				component.getPlugin().setOptions(options);
			else
				component.setOptions(options);
			return component;
		}
	}
}